Abstract classes
Abstract classes are a special type of base classes. These abstract class members are methods and properties declared without an implementation. All classes derived directly from abstract classes must implement these abstract methods and properties.
Program on abstract class
using System;
abstract class abc
{
protected int l, b;
public void get(int x, int y)
{
l = x;
b = y;
}
public abstract void put();
}
class xyz:abc
{
public override void put()
{
Console.WriteLine("Area of rectange" + (l * b));
}
}
class vision
{
public static void Main(String[] args)
{
xyz x = new xyz();
x.get(10, 5);
x.put();
}
}
Program on abstract classes and polymorphism.
using System;
abstract class abc
{
protected int b,h;
public void get(int x, int y)
{
b = x;
h = y;
}
public abstract void put();
}
class xyz:abc
{
public override void put()
{
Console.WriteLine("Area of Triangle" + (b*h)/2);
}
}
class vision
{
public static void Main(String[] args)
{
xyz x = new xyz();
abc z;
z = x; // polymorphisam
z.get(5, 3);
z.put();
}
}
Program to find the area of triangle, circle, simple interest using polymorphism.
using System;
abstract class abc
{
protected int b,h;
public void get(int x, int y)
{
b = x;
h = y;
Console.WriteLine("Area of Triangle" + (b * h) / 2);
}
public abstract void put();
}
class xyz:abc
{
int r;
public xyz(int x)
{
r = x;
}
public override void put()
{
Console.WriteLine("Area of Circle" + (22 / 7 * r * r));
}
}
class pqr:abc
{
int p,t,r;
public pqr(int x,int y,int z)
{
p=x;
t=y;
r=z;
}
public override void put()
{
Console.WriteLine("Simple Interest" + (p * t * r) / 100);
}
}
class vision
{
public static void Main(String[] args)
{
xyz x = new xyz(5);
pqr y = new pqr(1000,5,3);
abc z;
z = x; // polymorphisam
z.get(4, 5);
z.put();
z = y;
z.put();
}
}